package pl.edu.pw.perg.compinfodb.manager;

import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import pl.edu.pw.perg.compinfodb.dao.IPhysicalElementDAO;
import pl.edu.pw.perg.compinfodb.dao.IVirtualElementDAO;
import pl.edu.pw.perg.compinfodb.entities.PhysicalElement;
import pl.edu.pw.perg.compinfodb.entities.VirtualElement;

public class ComplexElementManager  implements IComplexElementManager {

	private IPhysicalElementDAO physicalElementDAO;
	private IVirtualElementDAO virtualElementDAO;
	
	@Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
	public void unboundElements(VirtualElement ve, PhysicalElement pe){
		if(ve != null){
			ve.setPhysicalElement(null);
			virtualElementDAO.merge(ve);
		}
		if(pe != null){
			pe.setVirtualElement(null);
			physicalElementDAO.merge(pe);
		}
		
	}
	@Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
	public void boundElements(VirtualElement ve, PhysicalElement pe){
		if(ve != null)
			ve.setPhysicalElement(pe);
		if(pe != null)
			pe.setVirtualElement(ve);
		physicalElementDAO.merge(pe);
		virtualElementDAO.merge(ve);
	}
	
	
	public void setPhysicalElementDAO(IPhysicalElementDAO physicalElementDAO) {
		this.physicalElementDAO = physicalElementDAO;
	}
	public void setVirtualElementDAO(IVirtualElementDAO virtualElementDAO) {
		this.virtualElementDAO = virtualElementDAO;
	}
	
	
//	private IVirtualElementManager virtualElementManager;
//	private IPhysicalElementManager physicalElementManager;
//	
//	public IVirtualElementManager getVirtualElementManager() {
//		return virtualElementManager;
//	}
//
//	public void setVirtualElementManager(
//			IVirtualElementManager virtualElementManager) {
//		this.virtualElementManager = virtualElementManager;
//	}
//
//	public IPhysicalElementManager getPhysicalElementManager() {
//		return physicalElementManager;
//	}
//
//	public void setPhysicalElementManager(
//			IPhysicalElementManager physicalElementManager) {
//		this.physicalElementManager = physicalElementManager;
//	}
//	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
//	public ComplexElement getComplexByVirtual(VirtualElement ve){
//		return ((ComplexElementDAO) getGenericDAO()).getComplexByVirtual(ve);
//	}
//	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
//	public ComplexElement getComplexByPhysical(PhysicalElement pe){
//		return ((ComplexElementDAO) getGenericDAO()).getComplexByPhysical(pe);
//	}
//	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
//	public PhysicalElement getPhysicalByVirtual(VirtualElement ve){
//		ComplexElement ce = getComplexByVirtual(ve);
//		return ce == null ? null : physicalElementManager.findById(ce.getPeId());
//	}
//	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
//	public VirtualElement getVirtualByPhysical(PhysicalElement pe){
//		ComplexElement ce = getComplexByPhysical(pe);
//		return ce == null ? null : virtualElementManager.findById(ce.getVeId());
//	}
}
